using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Publicuse.Appication;
using Publicuse.Autofac;
using Publicuse.Entity;
using Publicuse.Entity.AttributeUtil;
using Publicuse.Export;
using Publicuse.Interface;
using Publicuse.Interface.Extend;
using Publicuse.Util;
using ServiceStack;
using Geekeradmin.Authorize;
using Geekeradmin.Controllers;

namespace Geekeradmin.Controllers
{
  /// <summary>
  /// 系统参数配置模块管理
  /// </summary>
  [UserAuthorize("Admin")]
  public class SpconfController : BaseController
  {
    public SpconfController(
      IAutofaceInstance autoface,
      IMySqlSugarService mySqlSugar,
      IWebHostEnvironment webHost,
      Microsoft.Extensions.Options.IOptions<DBConnectionOption> options,
      IBaseServiceExt baseServiceExt
    )
      : base(autoface, mySqlSugar, webHost, options, baseServiceExt) { }

    /// <summary>
    /// 获取系统参数详细信息
    /// </summary>
    /// <param name="Id"></param>
    /// <returns></returns>
    [HttpGet("GetListb")]
    public async Task<JsonResult> GetDetail(int Id)
    {
      CommonResult result = await SpconfApplication.GetApplication(this.container).GetDetail(Id);
      return new JsonResult(result);
    }

    /// <summary>
    /// 获取系统参数配置信息列表
    /// </summary>
    /// <param name="query"></param>
    /// <returns></returns>
    [HttpPost("GetList"), AllowAnonymous]
    public async Task<JsonResult> GetList(Spconf query)
    {
      CommonResult result = await SpconfApplication.GetApplication(this.container).GetList(query);
      return new JsonResult(result);
    }

    /// <summary>
    /// 获取系统登录页面信息
    /// </summary>
    /// <returns></returns>
    [HttpGet("GetIndexInfo"), AllowAnonymous]
    public async Task<JsonResult> GetIndexInfo()
    {
      CommonResult result = await SpconfApplication.GetApplication(this.container).GetIndexInfo();
      return new JsonResult(result);
    }

    /// <summary>
    /// 检查参数配置名称是否存在
    /// </summary>
    /// <param name="sName"></param>
    /// <param name="loginUserCode"></param>
    /// <param name="Id"></param>
    /// <returns></returns>
    [HttpGet("CheckName"), AllowAnonymous]
    public async Task<JsonResult> CheckName(string sName, string loginUserCode, string Id = "")
    {
      CommonResult result = await SpconfApplication
        .GetApplication(this.container)
        .CheckName(sName, loginUserCode, Id);
      return new JsonResult(result);
    }

    /// <summary>
    /// 检查参数配置编码是否存在
    /// </summary>
    /// <param name="sCode"></param>
    /// <param name="loginUserCode"></param>
    /// <param name="Id"></param>
    /// <returns></returns>
    [HttpGet("CheckCode"), AllowAnonymous]
    public async Task<JsonResult> CheckCode(string sCode, string loginUserCode, string Id = "")
    {
      CommonResult result = await SpconfApplication
        .GetApplication(this.container)
        .CheckCode(sCode, loginUserCode, Id);
      return new JsonResult(result);
    }

    /// <summary>
    /// 新增修改系统参数配置信息
    /// </summary>
    /// <param name="info"></param>
    /// <returns></returns>
    [HttpPost("AddOrUpdate")]
    public async Task<JsonResult> AddOrUpdate(Spconf info)
    {
      CommonResult result = await SpconfApplication
        .GetApplication(this.container)
        .AddOrUpdate(info);
      return new JsonResult(result);
    }

    /// <summary>
    /// 删除系统参数配置信息
    /// </summary>
    /// <param name="Id"></param>
    /// <param name="loginUserCode"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    [HttpGet("Delete")]
    public async Task<JsonResult> Delete(string Id, string loginUserCode, string type = "-9")
    {
      CommonResult result = await SpconfApplication
        .GetApplication(this.container)
        .Delete(Id, loginUserCode, type);
      return new JsonResult(result);
    }

    /// <summary>
    /// 还原系统参数配置删除信息
    /// </summary>
    /// <param name="Id"></param>
    /// <param name="loginUserCode"></param>
    /// <returns></returns>
    [HttpGet("Reduction"), AllowAnonymous]
    public async Task<JsonResult> Reduction(string Id, string loginUserCode)
    {
      CommonResult result = await SpconfApplication
        .GetApplication(this.container)
        .Reduction(Id, loginUserCode);
      return new JsonResult(result);
    }

    /// <summary>
    /// 导出数据信息
    /// </summary>
    /// <param name="query"></param>
    /// <returns></returns>
    [HttpGet("ExportData")]
    public async Task<IActionResult> ExportData([FromQuery] Spconf query)
    {
      return await base.InitExportData(
        this.webHost,
        this.mySqlSugar,
        query,
        "\\template\\report\\spconf.xls",
        "系统参数配置信息统计",
        (a, b, c) =>
        {
          var ms = SpconfExport.ExportData(a, b, c, out var msgb);
          return ms;
        }
      );
    }
  }
}
